/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIServer.java
 *
 * Created on Apr 20, 2011, 3:32:18 PM
 */
package horsechess.server.gui;

import horsechess.packet.PlayerPacket;
import horsechess.server.data.ClientHandler;
import horsechess.server.data.RoomManager;
import horsechess.server.data.Server;
import horsechess.server.data.ServerHandler;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Administrator
 */
public class GUIServer extends javax.swing.JFrame {

    /** Creates new form GUIServer */
    public GUIServer() {
        initComponents();
        serverHandler = new Server(this);
        updateServerInfo();
        hideShowChessMan(1, false);
        hideShowChessMan(2, false);
        hideShowChessMan(3, false);
        hideShowChessMan(4, false);
    }

    public void updateServerInfo() {
        txtServerIP.setText(serverHandler.serverAddr);
        //System.out.println("Address: " + serverHandler.serverAddr);
        txtServerPort.setText(serverHandler.port + "");
    }

    /**
     * Hide/Show Chessman
     * @param n
     * @param state
     */
    private void hideShowChessMan(int n, boolean state) {
        if (n == 1) {
            if (pnlPlayer1.isVisible() != state) {
                pnlPlayer1.setVisible(state);
            }
        }
        if (n == 2) {
            if (pnlPlayer2.isVisible() != state) {
                pnlPlayer2.setVisible(state);
            }
        }
        if (n == 3) {
            if (pnlPlayer3.isVisible() != state) {
                pnlPlayer3.setVisible(state);
            }
        }
        if (n == 4) {
            if (pnlPlayer4.isVisible() != state) {
                pnlPlayer4.setVisible(state);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        optionTab = new javax.swing.JTabbedPane();
        pnlConnect = new javax.swing.JPanel();
        lblServerIP = new javax.swing.JLabel();
        txtServerIP = new javax.swing.JTextField();
        lblServerIP1 = new javax.swing.JLabel();
        txtServerPort = new javax.swing.JTextField();
        txtServerStatus = new java.awt.TextField();
        lblServerIP2 = new javax.swing.JLabel();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        btnBroadcast = new javax.swing.JButton();
        pnlPlayer = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        btnApply2 = new javax.swing.JButton();
        pnlPlayer1 = new javax.swing.JPanel();
        lblPlayer1 = new javax.swing.JLabel();
        picChessman1 = new javax.swing.JLabel();
        radPriority1 = new javax.swing.JRadioButton();
        lblPlayerName1 = new javax.swing.JLabel();
        lblNoOfHorse1 = new javax.swing.JLabel();
        pnlPlayer2 = new javax.swing.JPanel();
        lblPlayer2 = new javax.swing.JLabel();
        picChessman2 = new javax.swing.JLabel();
        radPriority2 = new javax.swing.JRadioButton();
        lblPlayerName2 = new javax.swing.JLabel();
        lblNoOfHorse2 = new javax.swing.JLabel();
        pnlPlayer3 = new javax.swing.JPanel();
        lblPlayer3 = new javax.swing.JLabel();
        picChessman3 = new javax.swing.JLabel();
        radPriority3 = new javax.swing.JRadioButton();
        lblPlayerName3 = new javax.swing.JLabel();
        lblNoOfHorse3 = new javax.swing.JLabel();
        pnlPlayer4 = new javax.swing.JPanel();
        lblPlayer4 = new javax.swing.JLabel();
        picChessman4 = new javax.swing.JLabel();
        radPriority4 = new javax.swing.JRadioButton();
        lblPlayerName4 = new javax.swing.JLabel();
        lblNoOfHorse4 = new javax.swing.JLabel();
        cboRoom = new javax.swing.JComboBox();
        lblNoOfPlayers = new javax.swing.JLabel();
        lblRefresh = new javax.swing.JLabel();
        btnPlay = new javax.swing.JButton();
        pnlDice = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        radOneDice = new javax.swing.JRadioButton();
        radTwoDice = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        btnApplyDice = new javax.swing.JButton();
        guiDice = new horsechess.client.gui.GUIDice();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        optionTab.setBackground(new java.awt.Color(255, 255, 255));
        optionTab.setMaximumSize(new java.awt.Dimension(450, 320));
        optionTab.setMinimumSize(new java.awt.Dimension(450, 320));

        pnlConnect.setBackground(new java.awt.Color(255, 255, 255));
        pnlConnect.setLayout(null);

        lblServerIP.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP.setText("Status");
        lblServerIP.setPreferredSize(new java.awt.Dimension(60, 14));
        pnlConnect.add(lblServerIP);
        lblServerIP.setBounds(20, 100, 60, 14);
        pnlConnect.add(txtServerIP);
        txtServerIP.setBounds(110, 40, 190, 20);

        lblServerIP1.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP1.setText("Server IP");
        pnlConnect.add(lblServerIP1);
        lblServerIP1.setBounds(20, 40, 90, 21);
        pnlConnect.add(txtServerPort);
        txtServerPort.setBounds(110, 70, 110, 20);

        txtServerStatus.setBackground(new java.awt.Color(51, 0, 51));
        txtServerStatus.setFont(new java.awt.Font("Dialog", 1, 12));
        txtServerStatus.setForeground(new java.awt.Color(0, 255, 153));
        pnlConnect.add(txtServerStatus);
        txtServerStatus.setBounds(110, 100, 300, 20);

        lblServerIP2.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP2.setText("Server Port");
        lblServerIP2.setPreferredSize(new java.awt.Dimension(60, 14));
        pnlConnect.add(lblServerIP2);
        lblServerIP2.setBounds(20, 70, 90, 14);

        btnStart.setBackground(new java.awt.Color(0, 153, 153));
        btnStart.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnStart.setForeground(new java.awt.Color(102, 51, 255));
        btnStart.setText("Start");
        btnStart.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 51, 0)));
        btnStart.setBorderPainted(false);
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });
        pnlConnect.add(btnStart);
        btnStart.setBounds(150, 170, 140, 40);

        btnStop.setBackground(new java.awt.Color(0, 153, 153));
        btnStop.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnStop.setForeground(new java.awt.Color(102, 51, 255));
        btnStop.setText("Stop");
        btnStop.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 102)));
        btnStop.setBorderPainted(false);
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });
        pnlConnect.add(btnStop);
        btnStop.setBounds(300, 170, 120, 40);

        btnBroadcast.setBackground(new java.awt.Color(0, 153, 153));
        btnBroadcast.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnBroadcast.setForeground(new java.awt.Color(102, 51, 255));
        btnBroadcast.setText("Broadcast");
        btnBroadcast.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 51, 0)));
        btnBroadcast.setBorderPainted(false);
        btnBroadcast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBroadcastActionPerformed(evt);
            }
        });
        pnlConnect.add(btnBroadcast);
        btnBroadcast.setBounds(10, 170, 130, 40);

        optionTab.addTab("Connection", null, pnlConnect, "Connection");

        pnlPlayer.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer.setMinimumSize(new java.awt.Dimension(450, 320));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Number of players");

        jLabel10.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel10.setForeground(new java.awt.Color(111, 74, 21));
        jLabel10.setText("No.");

        jLabel11.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel11.setForeground(new java.awt.Color(111, 74, 21));
        jLabel11.setText("Name");

        jLabel12.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel12.setForeground(new java.awt.Color(111, 74, 21));
        jLabel12.setText("Sample");

        jLabel13.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel13.setForeground(new java.awt.Color(111, 74, 21));
        jLabel13.setText("Number of horses");

        jLabel14.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel14.setForeground(new java.awt.Color(111, 74, 21));
        jLabel14.setText("Priority");

        btnApply2.setBackground(new java.awt.Color(0, 153, 153));
        btnApply2.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnApply2.setForeground(new java.awt.Color(102, 51, 255));
        btnApply2.setText("Apply");
        btnApply2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApply2btnApplyActionPerformed(evt);
            }
        });

        pnlPlayer1.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer1.setLayout(null);

        lblPlayer1.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer1.setText("1");
        pnlPlayer1.add(lblPlayer1);
        lblPlayer1.setBounds(0, 0, 7, 15);

        picChessman1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/GREEN.gif"))); // NOI18N
        picChessman1.setMaximumSize(new java.awt.Dimension(33, 33));
        picChessman1.setMinimumSize(new java.awt.Dimension(33, 33));
        picChessman1.setPreferredSize(new java.awt.Dimension(33, 33));
        pnlPlayer1.add(picChessman1);
        picChessman1.setBounds(160, 0, 23, 30);

        radPriority1.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(radPriority1);
        pnlPlayer1.add(radPriority1);
        radPriority1.setBounds(390, 0, 21, 21);

        lblPlayerName1.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer1.add(lblPlayerName1);
        lblPlayerName1.setBounds(40, 0, 100, 20);

        lblNoOfHorse1.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer1.add(lblNoOfHorse1);
        lblNoOfHorse1.setBounds(240, 0, 70, 20);

        pnlPlayer2.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer2.setLayout(null);

        lblPlayer2.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer2.setText("2");
        pnlPlayer2.add(lblPlayer2);
        lblPlayer2.setBounds(0, 0, 15, 15);

        picChessman2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/RED.gif"))); // NOI18N
        pnlPlayer2.add(picChessman2);
        picChessman2.setBounds(160, 0, 20, 20);

        radPriority2.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(radPriority2);
        pnlPlayer2.add(radPriority2);
        radPriority2.setBounds(390, 0, 21, 21);

        lblPlayerName2.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer2.add(lblPlayerName2);
        lblPlayerName2.setBounds(40, 0, 100, 20);

        lblNoOfHorse2.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer2.add(lblNoOfHorse2);
        lblNoOfHorse2.setBounds(240, 0, 70, 20);

        pnlPlayer3.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer3.setLayout(null);

        lblPlayer3.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer3.setText("3");
        pnlPlayer3.add(lblPlayer3);
        lblPlayer3.setBounds(0, 0, 24, 15);

        picChessman3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/YELLOW.gif"))); // NOI18N
        pnlPlayer3.add(picChessman3);
        picChessman3.setBounds(160, 0, 24, 24);

        radPriority3.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(radPriority3);
        pnlPlayer3.add(radPriority3);
        radPriority3.setBounds(390, 0, 21, 21);

        lblPlayerName3.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer3.add(lblPlayerName3);
        lblPlayerName3.setBounds(40, 0, 100, 20);

        lblNoOfHorse3.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer3.add(lblNoOfHorse3);
        lblNoOfHorse3.setBounds(240, 0, 70, 20);

        pnlPlayer4.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer4.setLayout(null);

        lblPlayer4.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer4.setText("4");
        pnlPlayer4.add(lblPlayer4);
        lblPlayer4.setBounds(0, 2, 16, 15);

        picChessman4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/BLUE.gif"))); // NOI18N
        pnlPlayer4.add(picChessman4);
        picChessman4.setBounds(160, 0, 24, 24);

        radPriority4.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(radPriority4);
        pnlPlayer4.add(radPriority4);
        radPriority4.setBounds(390, 0, 21, 21);

        lblPlayerName4.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer4.add(lblPlayerName4);
        lblPlayerName4.setBounds(40, 0, 110, 20);

        lblNoOfHorse4.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer4.add(lblNoOfHorse4);
        lblNoOfHorse4.setBounds(240, 0, 70, 20);

        cboRoom.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboRoom.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboRoomItemStateChanged(evt);
            }
        });
        cboRoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboRoomActionPerformed(evt);
            }
        });

        lblRefresh.setFont(new java.awt.Font("Verdana", 1, 14));
        lblRefresh.setForeground(new java.awt.Color(51, 51, 0));
        lblRefresh.setText("Refresh");
        lblRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblRefreshMouseClicked(evt);
            }
        });

        btnPlay.setBackground(new java.awt.Color(0, 153, 153));
        btnPlay.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnPlay.setForeground(new java.awt.Color(102, 51, 255));
        btnPlay.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 102)));
        btnPlay.setBorderPainted(false);
        btnPlay.setLabel("Play");
        btnPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlayActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPlayerLayout = new javax.swing.GroupLayout(pnlPlayer);
        pnlPlayer.setLayout(pnlPlayerLayout);
        pnlPlayerLayout.setHorizontalGroup(
            pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPlayerLayout.createSequentialGroup()
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPlayerLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(cboRoom, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNoOfPlayers, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
                    .addGroup(pnlPlayerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlPlayer1, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(pnlPlayer2, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(pnlPlayer3, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(pnlPlayer4, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addGroup(pnlPlayerLayout.createSequentialGroup()
                                .addGap(68, 68, 68)
                                .addComponent(btnApply2, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlPlayerLayout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel11)
                                .addGap(63, 63, 63)
                                .addComponent(jLabel12)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel13)
                                .addGap(32, 32, 32)
                                .addComponent(jLabel14)
                                .addGap(21, 21, 21)))))
                .addContainerGap())
        );
        pnlPlayerLayout.setVerticalGroup(
            pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPlayerLayout.createSequentialGroup()
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPlayerLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(cboRoom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNoOfPlayers, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel10)
                    .addComponent(jLabel14)
                    .addComponent(jLabel11)
                    .addComponent(jLabel13)
                    .addComponent(jLabel12))
                .addGap(18, 18, 18)
                .addComponent(pnlPlayer1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlPlayer2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlPlayer3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlPlayer4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnApply2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        optionTab.addTab("Player", pnlPlayer);

        pnlDice.setBackground(new java.awt.Color(255, 255, 255));

        jLabel7.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel7.setForeground(new java.awt.Color(0, 51, 255));
        jLabel7.setText("Dice chosen");

        radOneDice.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(radOneDice);
        radOneDice.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        radOneDice.setSelected(true);
        radOneDice.setText("One dice");
        radOneDice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radOneDiceActionPerformed(evt);
            }
        });

        radTwoDice.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(radTwoDice);
        radTwoDice.setFont(new java.awt.Font("Tahoma", 0, 12));
        radTwoDice.setText("Two dice");
        radTwoDice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radTwoDiceActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel8.setForeground(new java.awt.Color(0, 51, 255));
        jLabel8.setText("Image demonstration");

        btnApplyDice.setText("Apply");
        btnApplyDice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApplyDicebtnApplyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDiceLayout = new javax.swing.GroupLayout(pnlDice);
        pnlDice.setLayout(pnlDiceLayout);
        pnlDiceLayout.setHorizontalGroup(
            pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDiceLayout.createSequentialGroup()
                .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(radTwoDice)
                            .addComponent(radOneDice))
                        .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(pnlDiceLayout.createSequentialGroup()
                                .addGap(116, 116, 116)
                                .addComponent(jLabel8))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDiceLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(guiDice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(19, 19, 19))))
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(144, 144, 144)
                        .addComponent(btnApplyDice, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        pnlDiceLayout.setVerticalGroup(
            pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDiceLayout.createSequentialGroup()
                .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(radTwoDice)
                            .addComponent(guiDice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(radOneDice)))
                .addGap(18, 18, 18)
                .addComponent(btnApplyDice, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(90, Short.MAX_VALUE))
        );

        optionTab.addTab("Dice", null, pnlDice, "Dice");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(optionTab, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(optionTab, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void radOneDiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radOneDiceActionPerformed
        if (radOneDice.isSelected()) {
            guiDice.setNumberOfDice(1);
            guiDice.localizeDice();
        }
}//GEN-LAST:event_radOneDiceActionPerformed

    private void radTwoDiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radTwoDiceActionPerformed
        if (radTwoDice.isSelected()) {
            guiDice.setNumberOfDice(2);
            guiDice.localizeDice();
        }
}//GEN-LAST:event_radTwoDiceActionPerformed

    private void btnApplyDicebtnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyDicebtnApplyActionPerformed
        //Configuration player
        String room = (String) cboRoom.getSelectedItem();
        RoomManager rManeger = serverHandler.getRoom(room);
        rManeger.handleDice();
}//GEN-LAST:event_btnApplyDicebtnApplyActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        guiDice.loadImageDice1(1);
        guiDice.loadImageDice2(6);
        guiDice.localizeDice();
        picChessman1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/GREEN.gif")));
        picChessman2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/RED.gif")));
        picChessman3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/YELLOW.gif")));
        picChessman4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/BLUE.gif")));
    }//GEN-LAST:event_formWindowOpened

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        serverHandler.startReceivingServer();
        txtServerStatus.setText("Receiving client....");
        btnStart.setEnabled(false);
        cboRoom.setModel(new DefaultComboBoxModel(serverHandler.getRooms()));
        btnPlay.setEnabled(false);
        btnStop.setEnabled(true);
    }//GEN-LAST:event_btnStartActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        if (serverHandler.shutdownServer()) {
            btnBroadcast.setEnabled(true);
            btnStart.setEnabled(true);
            btnStop.setEnabled(false);
        } else {
            btnBroadcast.setEnabled(false);
            btnStart.setEnabled(false);
        }
    }//GEN-LAST:event_btnStopActionPerformed

    private void cboRoomItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboRoomItemStateChanged
        String roomName = (String) cboRoom.getSelectedItem();
        ArrayList clients = serverHandler.listClients(roomName);
        lblNoOfPlayers.setText(clients.size() + " players");
        for (int i = 0; i < clients.size(); i++) {
            ClientHandler client = (ClientHandler) clients.get(i);
            setUpPlayer(client.player);
        }
    }//GEN-LAST:event_cboRoomItemStateChanged

    private void btnBroadcastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBroadcastActionPerformed
        serverHandler.broadcast(txtServerIP.getText());
        btnBroadcast.setEnabled(false);
    }//GEN-LAST:event_btnBroadcastActionPerformed

    private void cboRoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboRoomActionPerformed
        displayRoomInfo();
    }//GEN-LAST:event_cboRoomActionPerformed

    private void lblRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblRefreshMouseClicked
        lblRefresh.setText("Refreshing.....");
        displayRoomInfo();
    }//GEN-LAST:event_lblRefreshMouseClicked

    private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayActionPerformed
        Vector<RoomManager> vectRoom = ServerHandler.listOfRoom;
        for (int i = 0; i < vectRoom.size(); i++) {
            RoomManager roomManager = vectRoom.elementAt(i);
            roomManager.startGame();
        }

}//GEN-LAST:event_btnPlayActionPerformed

    private void btnApply2btnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApply2btnApplyActionPerformed
        //Configuration player
        String roomName = (String) cboRoom.getSelectedItem();
        RoomManager room = serverHandler.getRoom(roomName);
        if (radPriority1.isSelected()) {
            room.broadcastMesage("@Priority#" + 1);
        }
        if (radPriority2.isSelected()) {
            room.broadcastMesage("@Priority#" + 2);
        }
        if (radPriority3.isSelected()) {
            room.broadcastMesage("@Priority#" + 3);
        }
        if (radPriority4.isSelected()) {
            room.broadcastMesage("@Priority#" + 4);
        }
}//GEN-LAST:event_btnApply2btnApplyActionPerformed

    public void displayRoomInfo() {

        hideShowChessMan(1, false);
        hideShowChessMan(2, false);
        hideShowChessMan(3, false);
        hideShowChessMan(4, false);
        Vector<RoomManager> vectRoom = ServerHandler.listOfRoom;
        for (int i = 0; i < vectRoom.size(); i++) {
            RoomManager roomManager = vectRoom.elementAt(i);
            if (cboRoom.getSelectedItem().equals(roomManager.getRoomName())) {
                lblNoOfPlayers.setText(roomManager.clients.size() + "");
                for (ClientHandler client : roomManager.clients) {
                    PlayerPacket player = client.player;
                    //System.out.println("Position of player: " + player.getPosOfPlayer());
                    switch (player.getPosOfPlayer()) {
                        case 0:
                            hideShowChessMan(1, true);
                            lblPlayerName1.setText(player.getPlayerName());
                            lblNoOfHorse1.setText(player.getNumberOfChessman() + "");
                            break;
                        case 1:
                            hideShowChessMan(2, true);
                            lblPlayerName2.setText(player.getPlayerName());
                            lblNoOfHorse2.setText(player.getNumberOfChessman() + "");
                            break;
                        case 2:
                            hideShowChessMan(3, true);
                            lblPlayerName3.setText(player.getPlayerName());
                            lblNoOfHorse3.setText(player.getNumberOfChessman() + "");
                            break;
                        case 3:
                            hideShowChessMan(4, true);
                            lblPlayerName4.setText(player.getPlayerName());
                            lblNoOfHorse4.setText(player.getNumberOfChessman() + "");
                            break;
                    }
                }
            }
        }
        lblRefresh.setText("Refresh");
        if (Integer.parseInt(lblNoOfPlayers.getText().trim()) >= 2) {
            btnPlay.setEnabled(true);
        }
    }

    private void setUpPlayer(PlayerPacket data) {
        int posOfPlayer = data.getPosOfPlayer() + 1;
        switch (posOfPlayer) {
            case 1:
                hideShowChessMan(1, true);
                lblPlayerName1.setText(data.getPlayerName());
                lblNoOfHorse1.setText(data.getNumberOfChessman() + "");
                break;
            case 2:
                hideShowChessMan(2, true);
                lblPlayerName2.setText(data.getPlayerName());
                lblNoOfHorse2.setText(data.getNumberOfChessman() + "");
                break;
            case 3:
                hideShowChessMan(3, true);
                lblPlayerName3.setText(data.getPlayerName());
                lblNoOfHorse3.setText(data.getNumberOfChessman() + "");
                break;
            case 4:
                hideShowChessMan(4, true);
                lblPlayerName4.setText(data.getPlayerName());
                lblNoOfHorse4.setText(data.getNumberOfChessman() + "");
                break;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUIServer().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApply2;
    private javax.swing.JButton btnApplyDice;
    private javax.swing.JButton btnBroadcast;
    public javax.swing.JButton btnPlay;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private javax.swing.ButtonGroup buttonGroup1;
    public javax.swing.JComboBox cboRoom;
    private horsechess.client.gui.GUIDice guiDice;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblNoOfHorse1;
    private javax.swing.JLabel lblNoOfHorse2;
    private javax.swing.JLabel lblNoOfHorse3;
    private javax.swing.JLabel lblNoOfHorse4;
    private javax.swing.JLabel lblNoOfPlayers;
    private javax.swing.JLabel lblPlayer1;
    private javax.swing.JLabel lblPlayer2;
    private javax.swing.JLabel lblPlayer3;
    private javax.swing.JLabel lblPlayer4;
    private javax.swing.JLabel lblPlayerName1;
    private javax.swing.JLabel lblPlayerName2;
    private javax.swing.JLabel lblPlayerName3;
    private javax.swing.JLabel lblPlayerName4;
    private javax.swing.JLabel lblRefresh;
    private javax.swing.JLabel lblServerIP;
    private javax.swing.JLabel lblServerIP1;
    private javax.swing.JLabel lblServerIP2;
    private javax.swing.JTabbedPane optionTab;
    private javax.swing.JLabel picChessman1;
    private javax.swing.JLabel picChessman2;
    private javax.swing.JLabel picChessman3;
    private javax.swing.JLabel picChessman4;
    private javax.swing.JPanel pnlConnect;
    private javax.swing.JPanel pnlDice;
    private javax.swing.JPanel pnlPlayer;
    private javax.swing.JPanel pnlPlayer1;
    private javax.swing.JPanel pnlPlayer2;
    private javax.swing.JPanel pnlPlayer3;
    private javax.swing.JPanel pnlPlayer4;
    public javax.swing.JRadioButton radOneDice;
    private javax.swing.JRadioButton radPriority1;
    private javax.swing.JRadioButton radPriority2;
    private javax.swing.JRadioButton radPriority3;
    private javax.swing.JRadioButton radPriority4;
    public javax.swing.JRadioButton radTwoDice;
    private javax.swing.JTextField txtServerIP;
    private javax.swing.JTextField txtServerPort;
    public static java.awt.TextField txtServerStatus;
    // End of variables declaration//GEN-END:variables
    private Server serverHandler;
}
